<template>
	<view class="u-wrap">
		<u-navbar :border-bottom="false" title="分类" :is-back="false">
			<view class="slot-wrap">
				<view class="search-icon"><u-icon name="search" class="search-icon-search" color="#BEBDBD" size="28" @click="$u.route('/pages/other/search/search')"></u-icon></view>
				<!-- <u-search @click="$u.route('/pages/other/search/search')" disabled placeholder="请输入商品名字" :show-action="false"></u-search> -->
			</view>
		</u-navbar>
		<view class="loading" v-if="loading">
			<u-loading></u-loading>
		</view>
		<view class="u-menu-wrap" v-else>
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in leftNav" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.className}}</text>
				</view>
			</scroll-view>
			<scroll-view scroll-y class="right-box" v-if="rightList != ''">
				<view class="page-view">
					<view class="class-item" v-for="(item,index) in rightList" :key="index">
						<view class="item-title">
							<u-divider color="#424242" border-color="#E8E8E8">{{item.goodsClass.className}}</u-divider>
							
						</view>
						<view class="item-container">
							<block v-if="item.subClasses != ''">
								<view @click="$u.route('/pages/purchase/productList/productList', {
					key: item.goodsClass.className,
					goodsClassId:item1.id
				})"
								 class="thumb-box" v-for="(item1, index1) in item.subClasses" :key="index1">
									<u-image border-radius="10rpx" width="120rpx" height="120rpx" :src="item1.icon ? imageURL + item1.icon : 'https://photo.hiyipin.net/USER/a25e14f2-9b4f-47ea-9cca-0ec33bb4e809.png'"></u-image>
									<view class="item-menu-name">{{item1.className}}</view>
								</view>
							</block>
							<view class="right-loading" v-if="item.subClasses == ''">
								<u-empty text="暂无该分类"></u-empty>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="right-box" style="width: 100%;" v-else>
				<view class="page-view">
					<view class="right-loading" style="padding: 0;height: 100vh;" v-if="rightLoading || rightList == ''">
						<u-loading :show="rightLoading"></u-loading>
						<u-empty :show="!rightLoading && rightList == ''"></u-empty>
					</view>
				</view>
			</view>
		</view>
		<!-- <u-tabbar
			:before-switch="tabChange"
			:show="tabBarState"
			inactive-color="#A5A4A4"
			active-color="#333333"
			icon-size="36"
			:list="tabBarList"
			mid-button
			mid-button-size="104"
			:border-top="false"
		></u-tabbar> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: true,
				rightLoading: true,
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				leftNav: [],
				rightList: [],
				imageURL: this.imageURL,
				tabBarState: false,
				// tabBarList: [
				// 	{
				// 		pagePath: '/pages/tabBar/index/index',
				// 		iconPath: '/static/tabBar/index-tab.png',
				// 		selectedIconPath: '/static/tabBar/index-tabActive.png',
				// 		text: '首页'
				// 	},
				// 	{
				// 		pagePath: '/pages/tabBar/goodsType/goodsType',
				// 		iconPath: '/static/tabBar/index-tab-cate.png',
				// 		selectedIconPath: '/static/tabBar/index-tab-cate-active.png',
				// 		text: '分类'
				// 	},
				// 	{
				// 		iconPath: '/static/tabBar/main-tab-bar-icon.png',
				// 		selectedIconPath: '/static/tabBar/main-tab-bar-icon.png',
				// 		pagePath: '/pages/tabBar/newIndex/newIndex',
				// 		midButton: true
				// 	},
				// 	{
				// 		pagePath: '/pages/tabBar/goodsCart/goodsCart',
				// 		iconPath: '/static/tabBar/index-tab-cart.png',
				// 		selectedIconPath: '/static/tabBar/index-tab-cart-active.png',
				// 		text: '购物车'
				// 	},
				// 	{
				// 		pagePath: '/pages/tabBar/my/my',
				// 		iconPath: '/static/tabBar/index-tab-user.png',
				// 		selectedIconPath: '/static/tabBar/index-tab-user-active.png',
				// 		text: '个人'
				// 	}
				// ]
			}
		},
		computed: {

		},
		onReady() {
			// this.$nextTick(() => {
			// 	this.tabBarState = true;
			// });
		},
		onLoad() {
			this.getLeftNav();
			
		},
		onPullDownRefresh(){
			 this.getLeftNav();
		},
		methods: {
			getLeftNav(){
				this.$u.get('goodsclass/getGoodsClassBylevel/1').then(res => {
					uni.stopPullDownRefresh();
					this.leftNav = res
					this.loading = false
					this.getRightList(res[0].id)
				})
			},
			getRightList(parentId) {
				this.$u.get('goodsclass/getSubClassByTopClass/' + parentId).then(res => {
					this.rightList = res
					this.rightLoading = false
				})
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
				this.rightLoading = true
				this.rightList = []
				this.getRightList(this.leftNav[this.current].id)
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			tabChange(index) {
				if (index == 3 || index == 4) {
					if (uni.getStorageSync('userInfo')) {
						return true;
					} else {
						this.$u.route('/pages/user/login/login');
						return false;
					}
				} else {
					return true;
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.loading {
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.right-loading {
		padding: 300rpx 0;
		display: flex;
		width: 100%;
		justify-content: center;
		align-items: center;
	}

	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	// .u-search-box {
	// 	padding: 18rpx 30rpx;
	// }

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	// .u-search-inner {
	// 	background-color: rgb(234, 234, 234);
	// 	border-radius: 100rpx;
	// 	display: flex;
	// 	align-items: center;
	// 	padding: 10rpx 16rpx;
	// }

	// .u-search-text {
	// 	font-size: 26rpx;
	// 	color: $u-tips-color;
	// 	margin-left: 10rpx;
	// }

	.u-tab-view {
		width: 200rpx;
		height: 100%;
		background-color: #f6f6f6;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		padding: 0 20rpx;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #FF3768;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.class-item:last-child {
		// min-height: 100vh;
		margin-bottom: 0;
	}


	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
		text-align: center;
		padding: 5rpx 10rpx;
		white-space: nowrap;
		width: 100%;
		text-overflow: ellipsis;
		overflow: hidden;
		word-break: break-all;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 33.333333%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	// .item-menu-image {
	// 	width: 120rpx;
	// 	height: 120rpx;
	// }

	.slot-wrap {
		width: 100%;
		.search-icon{
			position: relative;
			height: 88rpx;
		}
		.search-icon-search{
			position: absolute;
			right: 30rpx;
			top:30rpx;
		}
	}
</style>
